<?php

class admin{

	public static function login($username, $password){
		header("P3P:CP=CAO PSA OUR");
		$result['status'] = 0;
		$rs = db::find("select * from admin where username='$username'");
		if($rs){
			$password_encode = self::password_encode($username, $password, $rs['salt']);
			if($rs['status']!=1){
				$result['errMsg'] = "账号已关闭";
			}elseif($rs['password'] != $password_encode ){
				$result['errMsg'] = "用户名或密码错误";
			}
		}else{
			$result['errMsg'] = "用户名或密码错误";
		}
		if(empty($result['errMsg'])){
			$result['status'] = 1;
			$_SESSION['cp_islogin'] = true;
			$_SESSION['cp_mid'] = $rs['id'];
		}
		return $result;
	}
    
    public static function check_passwd($rs, $password) {
        header("P3P:CP=CAO PSA OUR");
		$bool = false;
		if($rs){
			$password_encode = self::password_encode($rs['username'], $password, $rs['salt']);
			if($rs['password'] == $password_encode ){
				$bool = true;
			}
		}
        return $bool;
    }

    public static function logout(){
		unset( $_SESSION['cp_islogin'] );
		unset( $_SESSION['cp_mid'] );
	}

	public static function check(){
        
		if(isset($_SESSION['cp_islogin']) && $_SESSION['cp_islogin']==true){
			return true;
		}
		return false;
	}

	public static function get(){
		if(isset($_SESSION['cp_islogin']) && $_SESSION['cp_islogin']==true ){
			$result = db::find("select id,username,realname from admin where id='$_SESSION[cp_mid]' AND status=1 ");
			if($result){
				return $result;
			}
		}
		echo "<script>window.location.href='/".APPNAME."/?m=authorized&a=login';</script>";
		exit;
	}

	public static function password_encode($username, $password, $salt){
		return md5($username.md5($password).$salt);
	}

}
